package pay

import (
	"context"
	"git.900sui.cn/kc/base/common/functions"
	"git.900sui.cn/kc/base/common/toolLib"
	"git.900sui.cn/kc/rpcPay/common/lib"
	"git.900sui.cn/kc/rpcPay/common/models"
	_const "git.900sui.cn/kc/rpcPay/lang/const"
	"git.900sui.cn/kc/rpcinterface/interface/bus"
	"git.900sui.cn/kc/rpcinterface/interface/order"
	"git.900sui.cn/kc/rpcinterface/interface/pay"
	"github.com/shopspring/decimal"
	"github.com/tidwall/gjson"
)

/**
 * @className xindalu
 * @author shijun<986244259@qq.com>
 * @date 2021-07-09
 */

type xindalupay struct {
}

//生成二维码
func (x xindalupay) PayQr(info *pay.PayInfo) (string, error) {
	//根据类型返回 前端地址
	err := lib.Xindalupay.Init(info.Cid)
	if err != nil {
		return "", err
	}
	return lib.Xindalupay.PayQr(info)
}

//h5支付-收银台
func (x xindalupay) PayH5(info *pay.PayInfo) (string, error) {
	return "", nil
}

//h5 升级版本
func (x xindalupay) PayH5New(info *pay.PayInfo, res *pay.PayH5) error {
	return nil
}

//微信小程序付款
func (x xindalupay) PayWxApp(info *pay.PayInfo) (*gjson.Result, error) {
	err := lib.Xindalupay.Init(info.Cid)
	if err != nil {
		return nil, err
	}
	return lib.Xindalupay.PayWxProgram(info)
}

func (x xindalupay) PayWxOfficial(info *pay.PayInfo) (*gjson.Result, error) {
	err := lib.Xindalupay.Init(info.Cid)
	if err != nil {
		return nil, err
	}
	return lib.Xindalupay.PayWxOfficial(info)
}

// APP支付【微信/支付宝】-直连
func (x xindalupay) PayApp(info *pay.PayInfo) (*gjson.Result, error) {
	err := lib.Xindalupay.Init(info.Cid)
	if err != nil {
		return nil, err
	}
	if info.ChosePayType == order.CHOSE_PAY_TYPE_PINGAN_WX {
		return lib.Xindalupay.PayWxApp(info)
	} else {
		return lib.Xindalupay.PayAliApp(info)
	}

}

func (x xindalupay) PayAppSign(info *pay.PayInfo) (string, error) {
	return "", nil
}

func (x xindalupay) Notify(data string) (*pay.PayNotify, error) {
	sourcegJson := gjson.Parse(data)
	//区域
	channelArea := int(sourcegJson.Get("channelArea").Int())
	channelPay := sourcegJson.Get("channelPay").String()
	err := lib.Xindalupay.Init(channelArea)
	if err != nil {
		return nil, err
	}
	gData, err1 := lib.Xindalupay.Notify(data, channelPay)
	if err1 != nil {
		return nil, err1
	}
	var payTime int64
	var orderSn, paySn, payFee, payAmount string
	var payChannel, payType int
	if channelPay == lib.CHANNELPAY_xindalu {
		//新大陆云收款
		//验证订单支付状态
		if gData.Get("ORDER_TIME").String() == "" {
			return nil, toolLib.CreateKcErr(_const.PAY_ERROR)
		}
		//logs.Info(gData.Get("ORDER_TIME").String())
		//付款时间 2006/01/02 15:04:05 2006-01-02 15:04:05 20060102 15:04:05
		payTime = functions.StrtoTime(gData.Get("ORDER_TIME").String(), "20060102150405")
		//金额-分转换
		fen := decimal.NewFromInt(100)
		//付款金额-渠道返回单位：分
		amount := decimal.NewFromInt(gData.Get("NETR_AMT").Int()).Div(fen)
		payAmount = amount.String()
		//新大陆银行云收款手续费
		//商户手续费
		payeeAmount := decimal.NewFromInt(gData.Get("CUST_FEE").Int()).Div(fen)
		payFee = payeeAmount.String()

		//商户订单号 这里需要处理一下 商户订单号 必须是order的原始订单号信息
		orderSn = gData.Get("THREE_ORDER_NO").String()
		order_sn, err := lib.Xindalupay.SelectThirdOrderSn(orderSn)
		if err != nil {
			return nil, err
		}
		orderSn = order_sn.(string)
		//需要查询一下 我们平台的订单号信息
		//支付渠道支付流水号
		paySn = gData.Get("ORDER_NO").String()
		payChannel = order.PAY_CHANNEL_xindalu
		payType = int(gData.Get("PAY_WAY").Int())
	}
	return &pay.PayNotify{
		OrderSn:    orderSn,
		PayTime:    payTime,
		PayAmount:  payAmount,
		PayFee:     payFee,
		PaySn:      paySn,
		PayStatus:  pay.PAY_Suc,
		PayChannel: payChannel,
		PayType:    payType,
	}, nil
}

func (x xindalupay) NotifyResponse(ctx context.Context, args *pay.ArgsNotify) (*pay.ReplyNotifyResponse, error) {
	return nil, nil
}

//异步回调响应
func (x xindalupay) PayFund(notify *pay.PayNotify, bill *order.ReplyGetOrderSplitBill) []bus.FundRecordItem {
	return []bus.FundRecordItem{
		{
			OrderSn:  notify.OrderSn,
			Amount:   notify.PayAmount,
			FundType: bus.FUND_TYPE_pay_in,
		}, {
			OrderSn:  notify.OrderSn,
			Amount:   notify.PayFee,
			FundType: bus.FUND_TYPE_bankfee_out,
		}, {
			OrderSn:  notify.OrderSn,
			Amount:   decimal.NewFromFloat(bill.InsureAmount).String(),
			FundType: bus.FUND_TYPE_insurance_out,
		}, {
			OrderSn:  notify.OrderSn,
			Amount:   decimal.NewFromFloat(bill.RenewInsureAmount).String(),
			FundType: bus.FUND_TYPE_reinsurance_out,
		}, {
			OrderSn:  notify.OrderSn,
			Amount:   decimal.NewFromFloat(bill.PlatformAmount).String(),
			FundType: bus.FUND_TYPE_plat_out,
		}, {
			OrderSn:  notify.OrderSn,
			Amount:   decimal.NewFromFloat(bill.DepositAmount).String(),
			FundType: bus.FUND_TYPE_depository,
		}, {
			OrderSn:  notify.OrderSn,
			Amount:   decimal.NewFromFloat(bill.BusAmount).Sub(decimal.NewFromFloat(bill.DepositAmount)).String(),
			FundType: bus.FUND_TYPE_usable,
		},
	}
}

// 获取清分金额
func (x xindalupay) GetFundAmount(notify *pay.PayNotify, bill *order.ReplyGetOrderSplitBill) string {
	return ""
}

//获取支付方式
func (x xindalupay) GetType() int {
	return order.PAY_CHANNEL_xindalu
}

func (x xindalupay) AgentChannel(busClearingModel *models.BusClearingModel, list map[string]interface{}) (id int, err error) {
	return
}

func (x xindalupay) AgentChannelPlat(platClearingModel *models.PlatformClearingModel, list map[string]interface{}) (id int, err error) {
	return
}

func (x xindalupay) AgentChannelInsure(insureClearingModel *models.InsureClearingModel, list map[string]interface{}) (id int, err error) {
	return
}

func (x xindalupay) AgentChannelRenewInsure(renewInsureClearingModel *models.RenewInsureClearingModel, list map[string]interface{}) (id int, err error) {
	return
}

func (x xindalupay) GetInsureFund() bool {
	return false
}
